import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { Area, DictApi } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getAreaCode } from '#/api';

const switchValue = ref('');
const sys_yes_no = reactive<DictApi.SelectOption[]>([]);
DictResp(['sys_yes_no']).then((res) => {
  if (res.sys_yes_no) {
    sys_yes_no.push(...res.sys_yes_no);
  }
});
/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'attr1',
      dependencies: {
        triggerFields: ['attr1'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'locationId',
      dependencies: {
        triggerFields: ['locationId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'areaId',
      dependencies: {
        triggerFields: ['areaId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'areaCode',
      label: '库位编码',
      rules: 'required',
      formItemClass: 'col-span-5',
      componentProps: {
        placeholder: '请输入库位编码',
      },
      dependencies: {
        triggerFields: ['switch', 'areaId'],
        trigger(values) {
          values.areaCode = switchValue;
        },
        disabled(values) {
          return !!values.attr1;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      labelWidth: 70,
      formItemClass: 'col-start-6 col-span-2',
      dependencies: {
        triggerFields: ['areaId'],
        show(values) {
          return values.attr1 !== '1';
        },
      },
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getAreaCode().then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'areaName',
      label: '库位名称',
      rules: 'required',
      componentProps: {
        placeholder: '请输入库位名称',
      },
      formItemClass: 'col-span-7',
    },

    {
      component: 'ElInputNumber',
      fieldName: 'area',
      defaultValue: 0,
      label: '面积',
      componentProps: {
        min: 0,
        class: 'w-full',
      },
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'ElInputNumber',
      fieldName: 'maxLoa',
      defaultValue: 0,
      label: '最大载重量',
      componentProps: {
        min: 0,
        class: 'w-full',
      },
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'RadioGroup',
      fieldName: 'enableFlag',
      label: '是否启用',
      componentProps: {
        options: sys_yes_no,
      },
      defaultValue: 'Y',
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'RadioGroup',
      fieldName: 'frozenFlag',
      label: '是否冻结',
      defaultValue: 'N',
      componentProps: {
        options: sys_yes_no,
      },
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'RadioGroup',
      fieldName: 'productMixing',
      label: '允许产品混放',
      defaultValue: 'N',
      componentProps: {
        options: sys_yes_no,
      },
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'RadioGroup',
      fieldName: 'batchMixing',
      label: '允许批次混放',
      defaultValue: 'N',
      componentProps: {
        options: sys_yes_no,
      },
      emptyStateValue: null,
      formItemClass: 'col-span-6',
    },
    {
      component: 'ElInputNumber',
      fieldName: 'positionX',
      defaultValue: 0,
      label: '库位位置X',
      componentProps: {
        min: 0,
        class: 'w-full',
      },
      emptyStateValue: null,
      formItemClass: 'col-span-4',
    },
    {
      component: 'ElInputNumber',
      fieldName: 'positionY',
      defaultValue: 0,
      label: '库位位置Y',
      componentProps: {
        min: 0,
        class: 'w-full',
      },
      emptyStateValue: null,
      formItemClass: 'col-span-4',
    },
    {
      component: 'ElInputNumber',
      fieldName: 'positionZ',
      defaultValue: 0,
      label: '库位位置Z',
      componentProps: {
        min: 0,
        class: 'w-full',
      },
      emptyStateValue: null,
      formItemClass: 'col-span-4',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注信息',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      emptyStateValue: null,
      formItemClass: 'col-span-12',
    },
  ];
}

/**
 * 获取表格列配置
 * @param onView 表格点击函数
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onView: (param: Area.Row) => void,
  _onActionClick?: OnActionClickFn<Area.Row>,
): VxeTableGridOptions<Area.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
      width: 50,
    },
    {
      field: 'areaCode',
      fixed: 'left',
      title: '库位编码',
      cellRender: { name: 'CellButtonInfo', props: { onClick: onView } },
      width: 150,
    },
    {
      field: 'areaName',
      title: '库位名称',
      width: 180,
    },
    {
      field: 'area',
      title: '面积',
      formatter: ({ cellValue }) => {
        return `${cellValue}m`;
      },
    },
    {
      field: 'maxLoa',
      title: '最大载重量',
    },
    {
      field: 'positionX',
      title: '库位位置X',
    },
    {
      field: 'positionY',
      title: '库位位置Y',
    },
    {
      field: 'positionZ',
      title: '库位位置Z',
    },
    {
      field: 'productMixing',
      title: '允许产品混放',
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'batchMixing',
      title: '允许批次混放',
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'enableFlag',
      title: '是否启用',
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'frozenFlag',
      slots: { default: 'frozenFlag' },
      title: '是否冻结',
      width: 150,
    },
    {
      field: 'remark',
      title: '备注',
      width: 180,
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
